<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          :data="list"
          :default-expand-all="true"
          :props="{
            children: 'children',
            label: 'name'
          }"
        >
          <template #default="{data}">
            <!-- 用一个行列布局 -->
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="Ddit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown></el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        :title="isEdit?'编辑':'添加子部门'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
      >
        <add-edit
          v-if="showDialog"
          :id="id"
          :is-edit="isEdit"
          :origin-list="originList"
          @success="hsuccess"
          @close="showDialog=false "
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartments, delDepartments } from '@/api/department'
import { transDataToTreeData } from '@/utils'
import AddEdit from './deDialog.vue'
export default {
  components: {
    AddEdit
  },
  data() {
    return {
      showDialog: false,
      // 依赖一份树形数据
      list: [], // 后台保存部门列表数据
      id: '', // 点击部门的id
      isEdit: false, // false 是新增 true 是编辑
      originList: []
    }
  },
  created() {
    this.loaddepartment()
  },
  methods: {
    // 关闭 弹窗  更新列表数据
    hsuccess() {
      this.showDialog = false
      this.loaddepartment()
    },
    // 删除
    hDel(id) {
      this.$confirm('请确定要删除吗').then(async() => {
        const res = await delDepartments(id)
        console.log(res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 更新 列表数据
        this.loaddepartment()
      }).catch(err => err)
    },
    // 编辑
    Ddit(id) {
      this.showDialog = true
      this.isEdit = true
      this.id = id
    },
    hAdd(id) {
      this.isEdit = false
      this.showDialog = true
      this.id = id
    },
    // 加载部门列表
    async loaddepartment() {
      try {
        const res = await getDepartments()
        this.originList = res.data.depts.map(item => {
          return { name: item.name, pid: item.pid, id: item.id, code: item.code }
        })
        console.log(this.originList, 'this.originList')
        console.log(res, '获取部门信息')
        this.list = transDataToTreeData(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
